package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.service.*;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyCommentService strategyCommentService;

    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @GetMapping("/detail")
    public Object toasts(String id){
        Strategy strategy = strategyService.get(id);
        //阅读数+1
        strategyStatisVORedisService.viewnumIncrease(id, 1);
        return JsonResult.success(strategy);
    }
    @GetMapping("/query")
    public Object query(StrategyQuery qo){
        return JsonResult.success(strategyService.query(qo));
    }


    @GetMapping("/themes")
    public Object themes(){
        return JsonResult.success(strategyThemeService.list());
    }



    @RequireLogin
    @PostMapping("/addComment")
    public Object addComment(StrategyComment comment,@UserParam UserInfo userInfo){

        //设置用户信息
        //comment.setUserId(userInfo.getId());
        //comment.setNickname(userInfo.getNickname());

        //bean属性复制
        //前提: 属性名字必须一致的时候才可以复制
        //操作原理: javabean内省机制(反射)
        BeanUtils.copyProperties(userInfo, comment);
        comment.setUserId(userInfo.getId());
        strategyCommentService.save(comment);


        //评论数+1
        strategyStatisVORedisService.replynumIncrease(comment.getStrategyId(), 1);



        return JsonResult.success();
    }


    @GetMapping("/comments")
    public Object comments(StrategyCommentQuery qo){
        return JsonResult.success(strategyCommentService.query(qo));
    }


    @RequireLogin
    @PostMapping("/commentThumb")
    public Object commentThumb(String cid,@UserParam UserInfo userInfo){


        strategyCommentService.commentThumb(cid, userInfo.getId());

        return JsonResult.success();
    }





    @GetMapping("/statisVo")
    public Object statisVo(String sid){
        return JsonResult.success(strategyStatisVORedisService.getStrategyStatisVo(sid));
    }


    @RequireLogin
    @PostMapping("/favor")
    public Object favor(String sid, @UserParam UserInfo userInfo){

        boolean ret = strategyStatisVORedisService.favor(sid, userInfo.getId());

        return JsonResult.success(ret);
    }


    @RequireLogin
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String sid, @UserParam UserInfo userInfo){

        boolean ret = strategyStatisVORedisService.strategyThumbup(sid, userInfo.getId());

        return JsonResult.success(ret);
    }




}
